package nit.wave.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;
import nit.wave.model.Book;
import nit.wave.response.message.TextMessage;
import nit.wave.utils.MessageUtil;
import nit.wave.utils.MinRobotFaceUtil;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class TextService {

	public static String getResponse(Map<String, String> requestMap) {
		String respMessage = null;
		String fromUserName = requestMap.get("FromUserName");
		String toUserName = requestMap.get("ToUserName");

		// 回复文本消息
		TextMessage textMessage = new TextMessage();
		textMessage.setContent("您好这里是校网三江潮，消息我们已经收到，感谢您的留言！");
		textMessage.setToUserName(fromUserName);
		textMessage.setFromUserName(toUserName);
		textMessage.setCreateTime(new Date().getTime());
		textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
		textMessage.setFuncFlag(0);

		String content = requestMap.get("Content");
		if (content.startsWith("lib")) {
			if (content.length() > 14) {
				String temp = content.substring(4, 14);
				String pattern = "[0-9]+";
				Pattern p = Pattern.compile(pattern);
				Matcher m = p.matcher(temp);
				if (m.matches()) {
					String userId = content.substring(4, 14);
					String password = content.substring(15);
					LibraryService libraryService = new LibraryService();
					textMessage.setContent(libraryService.binding(fromUserName, userId, password));
					respMessage = MessageUtil.textMessageToXml(textMessage);
					return respMessage;
				}else{
					textMessage.setContent("学号输入有误！");
					respMessage = MessageUtil.textMessageToXml(textMessage);
					return respMessage;
				}
			} else {
				textMessage.setContent("格式错误");
				respMessage = MessageUtil.textMessageToXml(textMessage);
				return respMessage;
			}

		} else if (content.equals("mybook")) {
			LibraryService libraryService = new LibraryService();
			String result = libraryService.getBook(fromUserName);
			if (result.equals("你还没有绑定过图书馆")) {
				textMessage.setContent("你还没有绑定过图书馆");
				respMessage = MessageUtil.textMessageToXml(textMessage);
				return respMessage;
			}
			JSONObject node = JSONObject.fromObject(result);
			String message = node.getString("message");
			if (node.getInt("code") == 200) {
				if (message.equals("获取成功")) {
					String data = node.getString("data");
					Gson gson = new Gson();
					List<Book> books = gson.fromJson(data, new TypeToken<List<Book>>() {
					}.getType());
					StringBuffer buffer = new StringBuffer();
					for (Book book : books) {
						buffer.append("书名:").append(book.getTitle()).append("   条码号:").append(book.getCode()).append("   校验码:").append(book.getCheck()).append("归还日期:").append(book.getReturnDate());
						if (book.isRenew()) {
							buffer.append("【已续借】\n");
						} else {
							buffer.append("【未续借】\n");
						}
					}
					textMessage.setContent(buffer.toString());
					respMessage = MessageUtil.textMessageToXml(textMessage);
					return respMessage;
				} else {
					textMessage.setContent(node.getString("message"));
					respMessage = MessageUtil.textMessageToXml(textMessage);
					return respMessage;
				}
			} else {
				textMessage.setContent(node.getString("message"));
				respMessage = MessageUtil.textMessageToXml(textMessage);
				return respMessage;
			}
		} else if (content.startsWith("renew")) {
			String code = content.substring(6, 14);
			String check = content.substring(15);
			LibraryService libraryService = new LibraryService();
			textMessage.setContent(libraryService.renewBook(fromUserName, code, check));
			respMessage = MessageUtil.textMessageToXml(textMessage);
			return respMessage;
		}

		respMessage = MessageUtil.textMessageToXml(textMessage);
		return respMessage;
	}

	/*
	 * String keyWord = content.replaceAll("^翻译+", "").trim(); if
	 * ("".equals(keyWord)) { textMessage.setContent("请输入“翻译+你要翻译的内容”");
	 * respMessage = MessageUtil.textMessageToXml(textMessage); return
	 * respMessage; } else {
	 * textMessage.setContent(BaiduTranslateService.translate(keyWord));
	 * respMessage = MessageUtil.textMessageToXml(textMessage); return
	 * respMessage; } } else if (("四级".equals(content)) ||
	 * ("4".equals(content))) { // 创建图文消息 NewsMessage newsMessage = new
	 * NewsMessage(); newsMessage.setToUserName(fromUserName);
	 * newsMessage.setFromUserName(toUserName); newsMessage.setCreateTime(new
	 * Date().getTime());
	 * newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
	 * newsMessage.setFuncFlag(0);
	 * 
	 * List<Article> articleList = new ArrayList<Article>();
	 * 
	 * Article article1 = new Article(); article1.setTitle("全国大学英语四六级考试成绩查询");
	 * article1.setDescription("");
	 * article1.setPicUrl("http://apix.sinaapp.com/cet/img/logo.jpg");
	 * article1.setUrl("http://apix.sinaapp.com/cet/?appkey=trialuser");
	 * 
	 * articleList.add(article1);
	 * newsMessage.setArticleCount(articleList.size());
	 * newsMessage.setArticles(articleList); respMessage =
	 * MessageUtil.newsMessageToXml(newsMessage);
	 * 
	 * return respMessage; } else if (MinRobotFaceUtil.isQqFace(content)) {
	 * textMessage.setContent(content); respMessage =
	 * MessageUtil.textMessageToXml(textMessage); return respMessage; } else if
	 * ("图文消息".equals(content)) {
	 * 
	 * // 创建图文消息 NewsMessage newsMessage = new NewsMessage();
	 * newsMessage.setToUserName(fromUserName);
	 * newsMessage.setFromUserName(toUserName); newsMessage.setCreateTime(new
	 * Date().getTime());
	 * newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
	 * newsMessage.setFuncFlag(0);
	 * 
	 * List<Article> articleList = new ArrayList<Article>();
	 * 
	 * Article article1 = new Article();
	 * article1.setTitle("关于properties文件放置的位置"); article1.setDescription("");
	 * article1.setPicUrl("http://avatar.csdn.net/7/F/F/1_u011080848.jpg");
	 * article1
	 * .setUrl("http://blog.csdn.net/u011080848/article/details/17621011");
	 * 
	 * Article article2 = new Article(); article2.setTitle("Mysql 5.6 二次安装出错");
	 * article2.setDescription("");
	 * article2.setPicUrl("http://avatar.csdn.net/7/F/F/1_u011080848.jpg");
	 * article2
	 * .setUrl("http://blog.csdn.net/u011080848/article/details/16906255");
	 * 
	 * Article article3 = new Article(); article3.setTitle("java开发前奏");
	 * article3.setDescription("");
	 * article3.setPicUrl("http://avatar.csdn.net/7/F/F/1_u011080848.jpg");
	 * article3
	 * .setUrl("http://blog.csdn.net/u011080848/article/details/10018765");
	 * 
	 * articleList.add(article1); articleList.add(article2);
	 * articleList.add(article3);
	 * newsMessage.setArticleCount(articleList.size());
	 * newsMessage.setArticles(articleList); respMessage =
	 * MessageUtil.newsMessageToXml(newsMessage);
	 * 
	 * return respMessage; } else { textMessage.setContent(getMainMenu());
	 * respMessage = MessageUtil.textMessageToXml(textMessage); return
	 * respMessage; }
	 */

	public static String getMainMenu() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("你好！校网三江潮").append("\n\n");
		buffer.append("1  主页").append("\n");
		buffer.append("2  公交查询").append("\n");
		buffer.append("3  周边搜索").append("\n");
		buffer.append("4 CET查询").append("\n");
		buffer.append("5  经典游戏").append("\n");
		buffer.append("6  美女电台").append("\n");
		buffer.append("7  人脸识别").append("\n");
		buffer.append("8  聊天唠嗑").append("\n");
		buffer.append("9  关于我们" + MinRobotFaceUtil.emoji(0x1F48F)).append("\n\n");
		buffer.append("回复“菜单”显示此帮助菜单");
		return buffer.toString();
	}

}
